$().ready(function() {
    $( document ).tooltip();
    validateRule();
    var viewer = new Viewer(document.getElementById('showlicenseImg'));
});

$.validator.setDefaults({
    submitHandler : function() {
        edit();
    }
});
function validateRule() {
    var icon = "<i class='fa fa-times-circle'></i> ";
    $("#signupForm").validate({
        rules:{
            name:{
                required:true
            },
            cName:{
                required:true
            },
            addr:{
                required:true
            },
            mobile:{
                required:true
            }
        },
        messages:{
            name:{
                required:icon + "商户名称必填"
            },
            cName:{
                required:icon + "必须绑定小区"
            },
            addr:{
                required:icon + "商户地址必填"
            },
            mobile:{
                required:icon + "请输入联系方式"
            }
        }
    })
}
var g_status = 1;
function changeDiv(status){
    if(status == 1){
        g_status=1;
        $("#refuseReason").val('');
        $('#reasonDiv').hide();
    }else{
        g_status=2;
        $('#reasonDiv').show();
    }
}
function edit() {
    if($("#name").val()==''){
        parent.layer.alert("商户名称不能为空");
        return;
    }
    if($("#communityId").val()==''){
        parent.layer.alert("请绑定小区");
        return;
    }
    if($("#mobile").val()==''){
        parent.layer.alert("商户联系电话不能为空");
        return;
    }
    if($("#addr").val()==''){
        parent.layer.alert("商户地址不能为空");
        return;
    }
    $.ajax({
        cache : true,
        type : "POST",
        url : "/shop/update",
        data : $('#signupForm').serialize(),// 你的formid
        async : false,
        error : function(request) {
            parent.layer.alert("服务器异常，请联系管理员");
        },
        success : function(data) {
            if (data.code == 0) {
                if($("#textfield").val() != $("#licensePic").val()){//如果没有上传图片
                    uploadImg();
                }else{
                    parent.layer.msg("操作成功");
                    parent.reLoad();
                    var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
                    parent.layer.close(index);
                }
            } else {
                parent.layer.alert(data.msg)
            }
        }
    });

}

function uploadImg(){
    $("#signupForm").attr("enctype","multipart/form-data");//enctype="multipart/form-data"
    $("#signupForm").attr("action","/shop/upload.action");
    $("#signupForm").ajaxSubmit(function(message) {
        // 对于表单提交成功后处理，message为提交页面saveReport.htm的返回内容
        var response = JSON.parse(message);
        if(response.upflag == 'success'){
            parent.layer.msg("操作成功");
            parent.reLoad();
            var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
            parent.layer.close(index);
        }

    });
}
function showBig(){
    var licenseUrl = $("#licenseUrl").val();
    $("#showlicenseImg").attr("src",licenseUrl);
    $("#showlicenseImg").click();
    //parent.showBig(licenseUrl);
}
function chooseCommunity(){
    layer.open({
        type : 2,
        title : '选择小区',
        maxmin : true,
        shadeClose : false, // 点击遮罩关闭层
        offset:'5px',
        area : [ '600px', '440px' ],
        content : '/car/black/choose' // iframe的url
    });
}
function setComunityInfo(id,name){
    $("#communityId").val(id);
    $("#communityName").val(name);
}
function view(){
    $('#fileInput').click();
}
function cancel() {
    //将img的src属性赋值为空串
    var logoPath = $("#licenseUrl").val();
    document.getElementById("preViewImgId").src = logoPath;
    //选择文件框的value属性赋值为空串
    document.getElementById("textfield").value = logoPath.split("/")[3];
    $('#delImgInput').hide();
}
function checkFileName(target){
    var name=target.value;
    var fileName = name.substring(name.lastIndexOf(".")+1).toLowerCase();
    if(fileName !="jpg" && fileName !="jpeg" && fileName !="bmp" && fileName !="png" &&
        fileName !="JPG" && fileName !="JPEG" && fileName !="BMP" && fileName !="PNG"){
        parent.layer.alert("请选择支持的格式文件上传");
        target.value="";
        return
    }
    document.getElementById('textfield').value=target.files[0].name;
    $('#delImgInput').show();
    //图片预览
    var img = document.getElementById("preViewImgId");
    //建一条文件流来读取图片
    var reader = new FileReader();
    //根据url将文件添加的流中
    var file = document.getElementById("fileInput").files[0];
    reader.readAsDataURL(file);
    //实现onload接口
    reader.onload = function(e) {
        //获取文件在流中url
        url = reader.result;
        //将url赋值给img的src属性
        img.src = url;
    };


}
function view(){
    $('#fileInput').click();
}




